package com.carmanage.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.carmanage.domain.Car;
import com.carmanage.service.BusService;
import com.carmanage.util.PageController;

@Controller
public class BusController {
	private BusService busService;

	@Resource
	public void setBusService(BusService busService) {
		this.busService = busService;
	}

	public BusService getBusService() {
		return busService;
	}

	@RequestMapping(value = "/saveBus.do")
	public String save(@RequestParam("busPlate") String busPlate,
			@RequestParam("busBrand") String busBrand,
			@RequestParam("busSeat") int busSeat,
			@RequestParam("busRegister_date") String busRegister_date,
			@RequestParam("busInsurance_date") String busInsurance_date,
			@RequestParam("busDriver_license") String busDriver_license,
			@RequestParam("busDriving_license") String busDriving_license,
			HttpSession session) {
		Car bus = new Car(busPlate, busBrand, busSeat, busRegister_date,
				busInsurance_date, busDriver_license, busDriving_license);
		System.out.println(bus);
		busService.save(bus);
		PageController pc = (PageController) session.getAttribute("pc");
		int totalRows = busService.getTotalRows();
		int currPage = pc.getCurrentPage();
		PageController currPc = new PageController(totalRows, currPage);
		List<Car> buses = busService.showAllBuses(currPc);
		session.setAttribute("pc", currPc);
		session.setAttribute("buses", buses);
		getBuses(session);
		return "pages/busManage.jsp";
	}

	@RequestMapping(value = "/showBuses.do")
	public String showAllBuses(HttpSession session) {
		int totalRow = busService.getTotalRows();
		PageController pc = new PageController(totalRow, 1);
		List<Car> buses = busService.showAllBuses(pc);
		session.setAttribute("buses", buses);
		session.setAttribute("pc", pc);
		getBuses(session);
		return "pages/busManage.jsp";

	}

	@RequestMapping(value = "/changeBusPages.do")
	public String changePages(@RequestParam("currentPage") int currentPage,
			HttpSession session) {
		PageController pc = (PageController) session.getAttribute("pc");
		pc.setCurrentPage(currentPage);
		List<Car> buses = busService.showAllBuses(pc);
		session.setAttribute("buses", buses);
		return "pages/busManage.jsp";

	}

	@RequestMapping(value = "/deleteBus.do")
	public String deleteEmp(@RequestParam("plate") String plate,
			HttpSession session) {
		busService.deleteBusByString(plate);
		PageController pc = (PageController) session.getAttribute("pc");
		int totalRows = busService.getTotalRows();
		int currPage = pc.getCurrentPage();
		PageController currPc = new PageController(totalRows, currPage);
		List<Car> buses = busService.showAllBuses(currPc);
		session.setAttribute("buses", buses);
		session.setAttribute("pc", currPc);
		getBuses(session);
		return "pages/busManage.jsp";

	}

	@RequestMapping(value = "/busGroupSearch.do")
	public String groupSearch(@RequestParam("searchPlate") String searchPlate,
			@RequestParam("searchBrand") String searchBrand,
			@RequestParam("searchSeat") int searchSeat,
			@RequestParam("searchRegister_date") String searchRegister_date,
			@RequestParam("searchInsurance_date") String searchInsurance_date,
			HttpSession session) {
		Car bus = new Car(searchPlate, searchBrand, searchSeat,
				searchRegister_date, searchInsurance_date);
		int totalRow = busService.getGroupTotalRows(bus);
		PageController pc = new PageController(totalRow, 1);
		List<Car> buses = busService.groupSearch(bus, pc);
		session.setAttribute("buses", buses);
		session.setAttribute("pc", pc);
		return "pages/busManage.jsp";
	}

	public void getBuses(HttpSession session) {
		List<Object[]> plates = busService.getPlate();
		List<Object[]> brands = busService.getBrand();
		List<Object[]> seats = busService.getSeat();
		List<Object[]> register_dates = busService.getRegister_date();
		List<Object[]> insurance_dates = busService.getInsurance_date();
		session.setAttribute("plates", plates);
		session.setAttribute("brands", brands);
		session.setAttribute("seats", seats);
		session.setAttribute("register_dates", register_dates);
		session.setAttribute("insurance_dates", insurance_dates);
	}

	 @RequestMapping(value="/showBusModifyData.do")
	 public @ResponseBody Car showModifyData(@RequestParam("modifyBusPlate") String modifyBusPlate,HttpSession session){
	 	Car car=busService.showModifyData(modifyBusPlate);
	 	 return car;
	 }
	@RequestMapping(value = "/modiBus.do")
	public String modiCar(@RequestParam("modiPlate") String modiPlate,
			@RequestParam("modiBrand") String modiBrand,
			@RequestParam("modiSeat") int modiSeat,
			@RequestParam("modiRegister_date") String modiRegister_date,
			@RequestParam("modiInsurance_date") String modiInsurance_date,
			@RequestParam("modiDriver_license") String modiDriver_license,
			@RequestParam("modiDriving_license") String modiDriving_license,
			HttpSession session) {
		Car car = new Car(modiPlate, modiBrand, modiSeat, modiRegister_date,
				modiInsurance_date, modiDriver_license, modiDriving_license);
		busService.modiCar(car);
		PageController pc = (PageController) session.getAttribute("pc");
		int totalRows = busService.getTotalRows();
		int currPage = pc.getCurrentPage();
		PageController currPc = new PageController(totalRows, currPage);
		List<Car> buses = busService.showAllBuses(currPc);
		session.setAttribute("pc", currPc);
		session.setAttribute("buses", buses);
		getBuses(session);
		return "pages/busManage.jsp";
	}

}
